package com.tomtaw.web.callnumber.ui.home;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;

import com.elvishew.xlog.XLog;

import org.xwalk.core.CustomViewCallback;
import org.xwalk.core.XWalkJavascriptResult;
import org.xwalk.core.XWalkUIClient;
import org.xwalk.core.XWalkView;

public class XWUIClient extends XWalkUIClient {

    private static final String TAG = "XWalkUIClient";

    public XWUIClient(XWalkView view, Context context) {
        super(view);
        //LogUtils.setEnable(true);
       // LogUtils.setLogDir(context.getExternalCacheDir().getPath());
    }

    @Override
    public void onPageLoadStarted(XWalkView view, String url) {
        Log.i(TAG, "onPageLoadStarted " + url);
        XLog.tag(TAG).i( "onPageLoadStarted " + url);
        super.onPageLoadStarted(view, url);
    }

    @Override
    public void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {
        Log.i(TAG, "onPageLoadStopped " + url + ", " + status.toString());
        XLog.tag(TAG).i( "onPageLoadStopped " + url + ", " + status.toString());
        super.onPageLoadStopped(view, url, status);
    }

    @Override
    public boolean onJsAlert(XWalkView view, String url, String message, XWalkJavascriptResult result) {
        Log.i(TAG, "onJsAlert " + url + ", " + message);
        XLog.tag(TAG).i( "onJsAlert " + url + ", " + message);
        return super.onJsAlert(view, url, message, result);
    }

    @Override
    public boolean onJsConfirm(XWalkView view, String url, String message, XWalkJavascriptResult result) {
        Log.i(TAG, "onJsConfirm " + url + ", " + message);
        XLog.tag(TAG).i( "onJsConfirm " + url + ", " + message);
        return super.onJsConfirm(view, url, message, result);
    }

    @Override
    public boolean onJsPrompt(XWalkView view, String url, String message, String defaultValue, XWalkJavascriptResult result) {
        Log.i(TAG, "onJsPrompt " + url + ", " + message);
        XLog.tag(TAG).i( "onJsPrompt " + url + ", " + message);
        return super.onJsPrompt(view, url, message, defaultValue, result);
    }



    @Override
    public boolean onConsoleMessage(XWalkView view, String message, int lineNumber, String sourceId, ConsoleMessageType messageType) {
        Log.i(TAG, "onConsoleMessage " + message + ", " + lineNumber + ", " + sourceId + ", " + messageType.toString());
        XLog.tag(TAG).i( "onConsoleMessage " + message + ", " + lineNumber + ", " + sourceId + ", " + messageType.toString());
        //LogUtils.d(TAG,"onConsoleMessage " + message + ", " + lineNumber + ", " + sourceId + ", " + messageType.toString());
        return super.onConsoleMessage(view, message, lineNumber, sourceId, messageType);
    }

    @Override
    public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
        Log.i(TAG, "onShowCustomView " + requestedOrientation);
        XLog.tag(TAG).i( "onShowCustomView " + requestedOrientation);
        super.onShowCustomView(view, requestedOrientation, callback);
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        Log.i(TAG, "onShowCustomView");
        XLog.tag(TAG).i( "onShowCustomView");
        super.onShowCustomView(view, callback);
    }

    @Override
    public boolean onCreateWindowRequested(XWalkView view, InitiateBy initiator, ValueCallback<XWalkView> callback) {
        Log.i(TAG, "onCreateWindowRequested");
        XLog.tag(TAG).i( "onCreateWindowRequested");
        return super.onCreateWindowRequested(view, initiator, callback);
    }

    @Override
    public boolean onJavascriptModalDialog(XWalkView view, JavascriptMessageType type, String url, String message, String defaultValue, XWalkJavascriptResult result) {
        Log.i(TAG, "onJavascriptModalDialog " + type.toString() + ", " + url + ", " + message + ", " + defaultValue);
        XLog.tag(TAG).i( "onJavascriptModalDialog " + type.toString() + ", " + url + ", " + message + ", " + defaultValue);
        return super.onJavascriptModalDialog(view, type, url, message, defaultValue, result);
    }

    @Override
    public void onFullscreenToggled(XWalkView view, boolean enterFullscreen) {
        Log.i(TAG, "onFullscreenToggled " + enterFullscreen);
        XLog.tag(TAG).i( "onFullscreenToggled " + enterFullscreen);
        super.onFullscreenToggled(view, enterFullscreen);
    }

    @Override
    public void onHideCustomView() {
        Log.i(TAG, "onHideCustomView");
        XLog.tag(TAG).i("onHideCustomView");
        super.onHideCustomView();
    }

    @Override
    public void onIconAvailable(XWalkView view, String url, Message startDownload) {
        Log.i(TAG, "onIconAvailable " + url + ", " + startDownload.toString());
        XLog.tag(TAG).i("onIconAvailable " + url + ", " + startDownload.toString());
        super.onIconAvailable(view, url, startDownload);
    }

    @Override
    public void onJavascriptCloseWindow(XWalkView view) {
        Log.i(TAG, "onJavascriptCloseWindow");
        XLog.tag(TAG).i("onJavascriptCloseWindow");
        super.onJavascriptCloseWindow(view);
    }

    @Override
    public void onReceivedIcon(XWalkView view, String url, Bitmap icon) {
        Log.i(TAG, "onReceivedIcon " + url);
        XLog.tag(TAG).i("onReceivedIcon " + url);
        super.onReceivedIcon(view, url, icon);
    }

    @Override
    public void onReceivedTitle(XWalkView view, String title) {
        Log.i(TAG, "onReceivedTitle " + title);
        XLog.tag(TAG).i("onReceivedTitle " + title);
        super.onReceivedTitle(view, title);
    }

    @Override
    public void onRequestFocus(XWalkView view) {
        Log.i(TAG, "onRequestFocus");
        XLog.tag(TAG).i("onRequestFocus");
        super.onRequestFocus(view);
    }

    @Override
    public void onScaleChanged(XWalkView view, float oldScale, float newScale) {
        Log.i(TAG, "onScaleChanged " + oldScale + ", " + newScale);
        XLog.tag(TAG).i("onScaleChanged " + oldScale + ", " + newScale);
        super.onScaleChanged(view, oldScale, newScale);
    }

    @Override
    public void onUnhandledKeyEvent(XWalkView view, KeyEvent event) {
        Log.i(TAG, "onUnhandledKeyEvent " + event.getAction() + ", " + event.getKeyCode());
        XLog.tag(TAG).i("onUnhandledKeyEvent " + event.getAction() + ", " + event.getKeyCode());
        super.onUnhandledKeyEvent(view, event);
    }

    @Override
    public void openFileChooser(XWalkView view, ValueCallback<Uri> uploadFile, String acceptType, String capture) {
        Log.i(TAG, "openFileChooser " + acceptType + ", " + capture);
        XLog.tag(TAG).i("openFileChooser " + acceptType + ", " + capture);
        super.openFileChooser(view, uploadFile, acceptType, capture);
    }

    @Override
    public boolean shouldOverrideKeyEvent(XWalkView view, KeyEvent event) {
        Log.i(TAG, "shouldOverrideKeyEvent " + event.getAction() + ", " + event.getKeyCode());
        XLog.tag(TAG).i("shouldOverrideKeyEvent " + event.getAction() + ", " + event.getKeyCode());
        return super.shouldOverrideKeyEvent(view, event);
    }

    @Override
    protected Object getBridge() {
        Object obj = super.getBridge();
        if(obj != null) {
            Log.i(TAG, "getBridge " + obj.getClass().getSimpleName());
           // XLog.tag(TAG).i("getBridge " + obj.getClass().getSimpleName());
        } else {
            Log.i(TAG, "getBridge()");
          //  XLog.tag(TAG).i("getBridge()");
        }
        return obj;
    }
}